<?php
/**
 * 系统设置 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: mod_config.php 523 2010-06-23 10:06:24Z lisijie86 $
*/

!defined('IN_ADMIN') && exit('Access Denied!');

if ( !is_founder() ) { prompt(__('对不起，你没有权限执行这项操作。')); }

if ($ac == 'save') {

	initGP(array('addvar'), 'P', 'array');
	$config = array(
		'base' => (array)$_POST['base'],
		'seo' => (array)$_POST['seo'],
		'display' => (array)$_POST['display'],
		'upload' => (array)$_POST['upload'],
		'user' => (array)$_POST['user'],
		'myvar' => (array)$_POST['myvar'],
		'mail' => (array)$_POST['mail'],
	);
	//添加自定义变量
	if ( !empty($addvar['name']) ) {
		if ( !preg_match('/^[a-z_]+$/i', $addvar['name']) ) {
			prompt(__('自定义变量名 <b>%s</b> 不合法，必须由英文字母和下划线组成。',$addvar['name']));
		} elseif ( $db->fetch_one_array("SELECT * FROM ".tname('config')." WHERE `type` = 'myvar' AND `name` = '$addvar[name]'") ) {
			prompt(__('自定义变量名 <b>%s</b> 已存在。',$addvar['name']));
		}
		$db->insert('config', array('name'=>$addvar['name'], 'value'=>$addvar['value'], 'type'=>'myvar'));
	}

	if ( is_array($config['base']['cachemode']) ) {
		$cachemode = array();
		foreach ($config['base']['cachemode'] as $val) {
			if ( in_array($val, array('index','cate','post')) ) {
				$cachemode[] = $val;
			}
		}
		$config['base']['cachemode'] = implode('|',$cachemode);
	} else {
		$config['base']['cachemode'] = '';
	}
	if ( empty($config['base']['blogurl']) ) {
		$config['base']['blogurl'] = 'http://{host}'.substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/'));
	} else {
		if ( substr($config['base']['blogurl'],0,7) != 'http://' ) {
			$config['base']['blogurl'] = 'http://'.$config['base']['blogurl'];
		}
		$config['base']['blogurl'] = rtrim($config['base']['blogurl'],'/');
	}
	if ( empty($config['display']['pagesize']) ) {
		$config['display']['pagesize'] = 10;
	}
	if ( empty($config['display']['sidebar_num']) ) {
		$config['display']['sidebar_num'] = 10;
	}
	if ($config['seo']['urlmode'] == 1) {
		$base_root = str_replace('admin.php','',$_SERVER['PHP_SELF']);
		$content = "
<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteBase $base_root
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^.*$ index.php?$0 [L]
</IfModule>";
		file_put_contents(JBLOG_ROOT.'.htaccess',$content);
	} else {
		@unlink(JBLOG_ROOT.'.htaccess');
	}
	if ( empty($config['display']['timeformat']) ) {
		$config['display']['timeformat'] = 'Y-m-d H:i';
	}
	$config['display']['comment_limit'] = abs(intval($config['display']['comment_limit']));
	if ( !$config['display']['comment_limit'] ) {
		$config['display']['comment_limit'] = 0;
	}
	$config['display']['reverse_order'] = $config['display']['reverse_order'] ? 1 : 0;
	$config['display']['auto_excerpt'] = $config['display']['auto_excerpt'] ? 1 : 0;
	$config['display']['link_tag'] = $config['display']['link_tag'] ? 1 : 0;
	foreach ($config as $type => $vars) {
		foreach ($vars as $key => $val) {
			$key = check_str($key);
			$val = $val;
			if ($key == 'close_reason') $val = nl2br($val);
			$db->insert('config', array('name' => $key, 'value'=>$val, 'type'=>$type), true);
		}
	}
	$config['mail']['mailsendtype'] = $config['mail']['mailsendtype'] ? 1 : 0;
	$config['mail']['reply_mail'] = $config['mail']['reply_mail'] ? 1 : 0;
	$config['mail']['reply_mail_type'] = (int)$config['mail']['mailsendtype'];
	do_action('config_save');
	recache('config,mail');
	prompt(__('成功更改配置信息，你可能需要刷新一下缓存！'),'admin.php?mod=config');

} elseif ( $ac == 'delmyvar' ) {

	initGP('varname');
	$db->delete('config', array('name'=>$varname, 'type'=>'myvar'));
	recache('config');
	ajax_out(array('status'=>'success'));
	
} else {

	$base = $seo = $display = $upload = $user = $mail = $myvar = array();
	$query = $db->query("SELECT * FROM ".tname('config')." WHERE `type` IN ('base','seo','display','upload','user','mail','myvar')");
	while ($row = $db->fetch_array($query)) {
		$row['value'] = check_str($row['value']);
		${$row['type']}[$row['name']] = $row['value'];
	}
	if ( !isset($base['cachemode']) ) {
		$base['cachemode'] = array();
	} else {
		$base['cachemode'] = explode('|',$base['cachemode']);
	}
	$temp = str_replace(array('-','.'), '', $base['timezone']);
	$base['timezone'] < 0 ? ${'tz_0'.$temp} = 'selected' : ${'tz_'.$temp} = 'selected';

	include admin_tpl('config');
}
?>